/**
 * 企业首页展示 homePageCompany.js
 * 版本号： v1.0.0
 */
$(function () {
    //设置body宽度，避免样式变形
    var width = document.body.offsetWidth;
    $('#companyBodyId_').width(width);

    //获取起重机械警报参数
    $.ajax({
        url: CONTEXT_PATH + '/alarmSign/getAlarmData.do',
        data: {alarmCategory: "1"},
        dataType: 'json',
        success: function(data){
            if(data){
                $('#craneAlarmTotal').text(data.totalNum);
                $('#craneAlarmHandle').text(data.handleNum);
                $('#craneAlarmNoHandle').text(data.untreatedNum);
            }
        },
        error:function () {
            $('#craneAlarmTotal').text("0");
            $('#craneAlarmHandle').text("0");
            $('#craneAlarmNoHandle').text("0");
        }
    });

    //获取起扬尘警报参数
    $.ajax({
        url: CONTEXT_PATH + '/alarmSign/getAlarmData.do',
        data: {alarmCategory: "2"},
        dataType: 'json',
        success: function(data){
            if(data){
                $('#dustAlarmTotal').text(data.totalNum);
                $('#dustAlarmHandle').text(data.handleNum);
                $('#dustAlarmNoHandle').text(data.untreatedNum);
            }
        },
        error:function () {
            $('#dustAlarmTotal').text("0");
            $('#dustAlarmHandle').text("0");
            $('#dustAlarmNoHandle').text("0");
        }
    });

    //获取项目总数及分类占比
    $.ajax({
        url: CONTEXT_PATH + '/project/getPrTypeCount.do',
        dataType: 'json',
        success: function(data){
            if(data){
                createPrTypePie(data.total,data.municipal,data.house);
            }
        },
        error:function () {
            createPrTypePie(0,0,0);
        }
    });

    //获取劳务出勤数据
    $.ajax({
        url: CONTEXT_PATH + '/attend/workerStatistics.do',
        dataType: 'json',
        success: function(data){
            if(data){
                workerStatistics(data.historyPersonCount,data.todayAttendanceCount);
            }
        },
        error:function () {
            workerStatistics(0,0);
        }
    });

    //获取设备数量统计
    $.ajax({
        url: CONTEXT_PATH + '/dustProCheckPoint/getDeviceTotal.do',
        dataType: 'json',
        success: function(data){
            if(data){
                deviceStatistics(data.dustTotal,data.craneTotal,data.videoTotal);
            }
        },
        error:function () {
            deviceStatistics(0,0,0);
        }
    });

    //起重机械警报点击事件
    $('.craneOut_div').on('click',function () {
        mainMenuSelect("项目管理","警示信息","craneAlarm");
    })

    //扬尘警报点击事件
    $('.dustBox').on('click',function () {
        mainMenuSelect("项目管理","警示信息","dustAlarm");
    })

    //项目总数点击事件
    $('.workerCount_one').on('click',function () {
        mainMenuSelect("项目管理","项目列表","");
    })


});

/**
 * 构建工程分类占比图
 */
function createPrTypePie(total,municipal,house){
    //设置项目总数
    $('#projectCount').text(total);
    //工程分类及占比
    var myChart = echarts.init(document.getElementById('projectRatio_'));
    var option = {
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        color:['#3c90f7', '#55bfc0','#5ebe67','#37f6bd','#BAFF4A','#7ED2F6','#41F642'],
        series : [
            {
                name: '项目类别',
                type: 'pie',
                radius : '55%',
                center: ['40%', '43%'],
                data:[
                    {value:house, name:'房建'},
                    {value:municipal, name:'市政'},
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    myChart.setOption(option);
    myChart.on('click', function (params) {
        var projectType = params.name;
        if(projectType == "房建"){
            projectType = "fangjian";
        }
        if(projectType == "市政"){
            projectType = "shizheng";
        }
        mainMenuSelect("项目管理","项目列表",projectType);
    });
}

/**
 * 劳务人员出勤数据
 */
function workerStatistics(historyPersonCount,todayAttendanceCount){
    $('#historyAttendCount').html(historyPersonCount);
    $('#todayAttendanceCount').html(todayAttendanceCount);
}

/**
 * 构建设备数量统计图
 * @param dustTotal
 * @param craneTotal
 * @param videoTotal
 */
function deviceStatistics(dustTotal,craneTotal,videoTotal){
    var myChart1 = echarts.init(document.getElementById('deviceClassify_'));
    option1 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        color:['#3c90f7'],
        xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01]
        },
        yAxis: {
            type: 'category',
            data: ['起重机械', '视频监控', '扬尘']
        },
        series: [{
            name: '总数',
            type: 'bar',
            data: [dustTotal, craneTotal, videoTotal]
        }]
    };
    myChart1.setOption(option1);
    myChart1.on('click', function (params) {
        var deviceType = params.name;
        if(deviceType == "扬尘"){
            deviceType = "dustTotal";
        }
        if(deviceType == "视频监控"){
            deviceType = "videoTotal";
        }
        if(deviceType == "起重机械"){
            deviceType = "craneTotal";
        }
        mainMenuSelect("设备管理","设备管理",deviceType);
    })
}